package com.totoro.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import com.totoro.rabbitmq.RabbitMQConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * RabbitMQ消息监听类的一些配置
 * Created by Administrator on 2017/9/14.
 */
@Component
public class RabbitMQMsgListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQMsgListener.class);

    /**
     * 对RabbitMQ监听器的一些设置
     *
     * @param connectionFactory
     * @return
     */
    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);

        //设置消息自动确认
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
//        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        /*
            消费者一次性从队列中获取的消息个数，设置这儿参数提高RabbitMQ在高并发下处理消息的吞吐量
            也容易造成客户端消费不均衡的问题（此时将preferchCount设为1）

         */
//        simpleRabbitListenerContainerFactory.setPrefetchCount(10);

        return simpleRabbitListenerContainerFactory;
    }

    /**
     * 监听hello队列消息
     */
//    @RabbitListener(queues = {RabbitMQConstant.queue.QUEUE}, containerFactory = "simpleRabbitListenerContainerFactory")
    public void handleMessage_hello(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String msg = new String(message.getBody(), "utf-8");
        if (("unack".equals(msg))) {
            //不确认
            channel.basicNack(deliveryTag, false, true);
        } else {
            channel.basicAck(deliveryTag, false);
            System.out.println("hello 队列1收到消息：" + msg);
        }
    }

//    @RabbitListener(queues = {RabbitMQConfig.queue.QUEUE}, containerFactory = "simpleRabbitListenerContainerFactory")
//    public void handleMessage_hello_2(Message message, Channel channel) throws IOException{
//        System.out.println("hello 队列2收到消息："+message);
//    }

    /**
     * 监听add队列消息
     *
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {RabbitMQConstant.queue.QUEUE_ADD}, containerFactory = "simpleRabbitListenerContainerFactory")
    public void handleMessage_add(Message message, Channel channel) {
        System.out.println("add 队列消息：" + message);
    }

    //测试fanout多个consumer都能接受消息
    @RabbitListener(containerFactory = "simpleRabbitListenerContainerFactory", queues = {"EAS:PATCH:SERVICE:QUEUEB"})
    public void patchHadleMessage(Message message, Channel channel) {
        try {
            String msg = new String(message.getBody(), "utf-8");
            LOGGER.info("B 收到补丁服务的消息为：{}", msg);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
